﻿/// <reference path="MicrosoftAjax.js"/>
/// <reference path="MicrosoftAjaxTemplates.js"/>

function getChildren(parent) {
    /// <summary>Creates the list of class and namespace children of a namespace.</summary>
    /// <param name="parent"/>
    /// <returns type="Array"/>
    var children = [];
    for (var name in parent) {
        if (name[0] === '_') continue;
        var child = parent[name];
        if (child.getName && (
            Type.isNamespace(child) ||
            Type.isClass(child))) {

            children.push(child);
        }
    }
    return children.sort(function(a, b) {
        return a.getName() < b.getName() ? -1 : 1;
    });
}

function getMembers(type) {
    /// <summary>Gets the list of members of a type.</summary>
    /// <param name="type" type="Type">The type to explore.</param>
    /// <returns type="Array"/>
    var members = [];
    for (var name in type.prototype) {
        if (name[0] === '_' ||
                    name.startsWith("set_") ||
                    name.startsWith("remove_")) continue;
        members.push((typeof type.prototype[name] !== "function") ?
                        { type: "property", name: name} :
                    name.startsWith("get_") ?
                        { type: "property", name: name.substr(4)} :
                    name.startsWith("add_") ?
                        { type: "event", name: name.substr(4)} :
                        { type: "method", name: name }
                );
    }
    for (name in type) {
        if (name[0] === '_') continue;
        var member = type[name];
        if (typeof member === "function" && !Function.prototype[name]) {
            members.push({ type: "staticMethod", name: name });
        }
    }
    return members.sort(function(a, b) { return a.name < b.name ? -1 : 1; });
}

function getSimpleName(fullName) {
    /// <summary>Extracts the simple name of a type from its fully-qualified name.</summary>
    /// <param name="fullName" type="String"/>
    /// <returns type="String"/>
    return fullName.replace(/.+\.([^\.]+)/i, "$1");
}

function toggleVisibility(element) {
    /// <summary>Toggles the visibility of the child nodes of a tree node.</summary>
    /// <param name="element" domElement="true">The parent node.</param>
    var childList = element.parentNode.getElementsByTagName("ul")[0],
        isClosed = element.innerHTML === "+";
    childList.style.display = isClosed ? "block" : "none";
    element.innerHTML = isClosed ? "-" : "+";
    return false;
}

function onCommand(sender, args) {
    if (args.get_commandName() === "select") {
        var dataItem = sender.findContext(args.get_commandSource()).dataItem;
        Sys.Application.findComponent("details").set_data(dataItem);
    }
}

function onDetailsRendering(sender, args) {
    args.set_itemTemplate(Type.isNamespace(args.get_dataItem()) ?
        "#namespaceTemplate" : "#classTemplate");
}

function onNamespaceChildRendering(sender, args) {
    args.set_itemPlaceholder(
        Type.isClass(args.get_dataItem()) ?
            "#classPlaceHolder" :
            "#namespacePlaceHolder"
    );
}

function onClassMemberRendering(sender, args) {
    var memberType = args.get_dataItem().type;
    args.set_itemPlaceholder("#" + memberType + "PlaceHolder");
}